package gov.dhs.cis.soa.readers;

import gov.dhs.cis.soa.SOAData;
import gov.dhs.cis.soa.Utils;
import gov.dhs.cis.soa.data.DataClass;

import java.util.ArrayList;

import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;


public class DataClassReader extends ExcelReader {
	static final Logger loger = Logger.getLogger(DataClassReader.class);

	/*
	private final String DATA_FILE_NAME = "USCIS Architecture Components - Data Layer.xls";
	private String SHEET_NAME = "Unique Entities";
	
	private int ENM_COL_NUM = 0;
	private int CNM_COL_NUM = 1;
	private int WIPT_COL_NUM = 2;
	private int DEF_COL_NUM = 3;
	private int COMMENT_COL_NUM = 4;
	private int USED_COL_NUM = 5;
	private int OPER_COL_NUM = 6;
	private int APP_COL_NUM = 7;
	private int REL_COL_NUM = 9;
	
	private int MAX_ROWS = 139;
		
	public SOAData extractDataClasses(SOAData soaData) {
		loger.debug("Extracting Data sheet Layer Sheet...");
		HSSFWorkbook w = openFile(DATA_FILE_NAME);
		HSSFSheet s1 = w.getSheet(SHEET_NAME);
		int rows = s1.getLastRowNum();
		loger.debug("S1 rows =" + rows);
		
		ArrayList<DataClass> dataList = new ArrayList<DataClass>();
		soaData.dataList = dataList;
		for (int n = 2; n < MAX_ROWS; n++) {
			HSSFRow row = s1.getRow(n);
			if(row == null)
				continue;
			String eName = getCellStringValue(w, row.getCell(ENM_COL_NUM));;
			String cName = getCellStringValue(w, row.getCell(CNM_COL_NUM));;
			if(Utils.isEmpty(cName)) {	//Utils.isEmpty(eName) && 
				loger.warn("Class name for the Data Class at row:" + n + " is Empty, so skipping.");
				continue;
			}
			DataClass dataObj = new DataClass();
			dataObj.id = Utils.getNextId();
			dataObj.defination = getCellStringValue(w, row.getCell(DEF_COL_NUM));
			dataObj.comments = getCellStringValue(w, row.getCell(COMMENT_COL_NUM));
			dataObj.wipt = getCellStringValue(w, row.getCell(WIPT_COL_NUM));
			dataObj.release = getCellStringValue(w, row.getCell(REL_COL_NUM));
			String used = getCellStringValue(w, row.getCell(USED_COL_NUM));
			String applnm = getCellStringValue(w, row.getCell(APP_COL_NUM));
			
			dataObj.cname = cName.trim();
			dataObj.ename = eName.trim();
			if(!Utils.isEmpty(dataObj.wipt) && dataObj.wipt.length() > 3)
				dataObj.wipt = dataObj.wipt.trim().substring(0, 3);
			dataObj.release = Utils.convert2Release(dataObj.release);
			dataObj.used = used;
			dataObj.application = applnm;
			dataObj.operations = getCellStringValue(w, row.getCell(OPER_COL_NUM));

			dataObj.createDescription();
			dataList.add(dataObj);
		}
		closeAll(w);
		return soaData;
	}
	*/
	/*
	private final String DATA_FILE_NAME = "IOE_Business Domain Model_v29.xls";
	private String SHEET_NAME = "BDM Entity Definitions";
	
	private String ENM_COL_NUM = "A";
	private String COMMENT_COL_NUM = "B";
	private String TYPE_COL_NUM = "C";
	
		
	public SOAData extractDataClasses(SOAData soaData) {
		loger.debug("Extracting Data sheet (BDM) Layer Sheet...");
		HSSFWorkbook w = openFile(DATA_FILE_NAME);
		HSSFSheet s1 = w.getSheet(SHEET_NAME);
		int rows = s1.getLastRowNum();
		loger.debug("S1 rows =" + rows);
		
		ArrayList<DataClass> dataList = new ArrayList<DataClass>();
		soaData.dataList = dataList;
		
		boolean bEndLoop = false;
		int n = 0;	//row index
		while (!bEndLoop) {
			HSSFRow row = s1.getRow(n++);
			if(row == null)
				continue;
			String cName = getCellStringValue(w, row, ENM_COL_NUM);
			String comments = getCellStringValue(w, row, COMMENT_COL_NUM);
			String type = getCellStringValue(w, row, TYPE_COL_NUM);
			
			if(Utils.isEmpty(type)) { 
				loger.warn("BDM Entity name for the Data Class at row:" + n + " is Empty, so skipping.");
				continue;
			}
			if("END".equalsIgnoreCase(type)) {
				bEndLoop = true;
				continue;
			}
			if(DataClass.PERSISTED_TYPE.equalsIgnoreCase(type)) { 
				DataClass dataObj = new DataClass();
				dataObj.id = Utils.getNextId();
				dataObj.cname = cName.trim();
				dataObj.comments = comments;
				dataObj.stereoType = DataClass.PERSISTED_TYPE;
				
				dataObj.createDescription();
				dataList.add(dataObj);
			}
		}
		closeAll(w);
		return soaData;
	}
	*/
	private final String DATA_FILE_NAME = "Master Object Catalog - 2010-06-21.bdm.xls";
	private String[] SHEET_NAMES = { "IOE Data Entities", "Non-IOE Data Entities", "IOE Transfer Objects" };
	private int[] NO_OF_ROWS = { 60, 120, 185};
	
	private String ENM_COL_NUM = "F";
	private String COMMENT_COL_NUM = "G";	
	private String NON_DATA_SHEET_SOURCE_COL_NUM = "H";	
		
	public SOAData extractDataClasses(SOAData soaData) {
		loger.debug("Extracting Data sheet (BDM) Layer Sheet...");
		HSSFWorkbook w = openFile(DATA_FILE_NAME);
		
		ArrayList<DataClass> dataList = new ArrayList<DataClass>();
		soaData.dataList = dataList;

		for (int i = 0; i < SHEET_NAMES.length; i++) {
			String sheetName = SHEET_NAMES[i];
			HSSFSheet s1 = w.getSheet(sheetName);
			int rows = s1.getLastRowNum();
			loger.debug("S1 rows =" + rows);
			
			for(int n = 2; n < NO_OF_ROWS[i]; n++) {
				HSSFRow row = s1.getRow(n);
				if(row == null)
					continue;
				String cName = getCellStringValue(w, row, ENM_COL_NUM);
				String comments = getCellStringValue(w, row, COMMENT_COL_NUM);
				String type = getCellStringValue(w, row, NON_DATA_SHEET_SOURCE_COL_NUM);
				
				if(Utils.isEmpty(cName)) { 
					loger.warn("MOC Data Entity name for the Data Class at row:" + n + " is Empty, so skipping.");
					continue;
				}
				DataClass dataObj = new DataClass();
				dataObj.id = Utils.getNextId();
				dataObj.cname = cName.trim();
				dataObj.comments = comments;
				dataObj.stereoType = type;
				
				if("IOE Data Entities".equalsIgnoreCase(sheetName)) {
					dataObj.groupType = DataClass.PERSISTED_TYPE;
				} else if("IOE Transfer Objects".equalsIgnoreCase(sheetName)) {
					dataObj.groupType = DataClass.IOE_TO_TYPE;
					dataObj.stereoType = sheetName;
					dataObj.comments = "";
				} else {
					dataObj.groupType = DataClass.NON_IOE_TO_TYPE;
				}
				
				if(Utils.findDataByName(dataObj.stereoType, dataObj.cname, dataList) == null) {
					dataObj.createDescription();
					dataList.add(dataObj);
				} else {
					loger.warn("MOC Data Entity [" + dataObj.cname + "] for the Data Class at row:" + n + " is is already present in the list, so skipping.");
					continue;					
				}
			}
		}
		closeAll(w);
		return soaData;
	}
	
}
